package cn.com.org.dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;

public class MongoBase {
	private static MongoClient mongoClient = null;
	private MongoBase() {		
	}
	public static MongoDatabase getDB(String dbname) {
		if (mongoClient == null) {
			initDB();
		}
		return mongoClient.getDatabase(dbname);
	}
	private static void initDB() {
		String hostname = "127.0.0.1";
		int port = 27017;
		String conigPath="/config/db.properties";
		Properties prop = new Properties(); 
	    InputStream intream=Object.class.getResourceAsStream(conigPath);
	    try {
			prop.load(intream);
		    hostname=prop.getProperty("mongo_hostname");
		    String portstr=prop.getProperty("mongo_port");
		    port=Integer.parseInt(portstr);
		} catch (IOException e) {

		} 
		mongoClient=new MongoClient(hostname,port);
	}
}
